iT邦幫忙

2023 iThome 鐵人賽

DAY 7
0
SideProject30

python基礎及數據科學之應用系列 第 7

python基礎及數據科學之應用day 7[Python基礎應用(Requests模組進階嘗試及提取天文台資料)]

  • 分享至 

  • xImage
  •  

大家好,今天將會使用httpbin 向各位更好的介紹python requests 的get 和post,以及部份我之前用requests 查詢天氣的技巧。那麼便開始吧

http posts

在Python的requests庫中,該POST方法用於向指定的URL發送HTTP POST請求。此POST方法通常用於將資料傳送到伺服器以建立或更新資源。以下是如何使用該程式庫發出 HTTP POST 請求的範例requests:

import requests as req

payload={"name":"carsonleung","age":14}
url="https://httpbin.org/post"
response=req.post(url,data=payload)
print(response.text)

執行結果:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "age": "14", #answer
    "name": "carsonleung" #answer
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "23", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.31.0", 
    "X-Amzn-Trace-Id": "Root=1-650d2071-5b9338b000df385258cfedf2"
  }, 
  "json": null, 
  "origin": "34.168.185.12", 
  "url": "https://httpbin.org/post"
}

尋找網頁中的cookies

Cookie 是使用者造訪網站時儲存在使用者裝置(例如電腦或智慧型手機)上的小型文字檔案。它們由網站創建,用於儲存有關用戶在該網站上的活動的資訊(例如登入)。

(下列例子只作參考,不一定成功):

import requests as req

url="https://httpbin.org/"

response=req.get(url+"cookies")
print(response.text)

執行結果:

>{
  "cookies": {}
}

在真正網頁查詢cookies

import requests as req

url="https://www.youtube.com/"

response=req.get(url+"cookies")
cookies = response.cookies

# Print the cookies
for cookie in cookies:
    print(f'Name: {cookie.name}, Value: {cookie.value}')

執行結果:

>Name: VISITOR_INFO1_LIVE, Value: lztAmx2kNbw
>Name: VISITOR_PRIVACY_METADATA, Value: CgJVUxICGgA%3D
>Name: YSC, Value: JqKU-fyKGmk

python 中的 eval 是什麼

這裡,expression是一個表示有效 Python 表達式的字串。此eval()函數解析並計算表達式,並將結果值賦給變數result。

範例1:

x = 5
y = 10
result = eval('x + y')
print(result)  # Output: 15

執行結果:

>>15

範例2:

expression = '[1, 2, 3, 4, 5]'
result = eval(expression)
print(result)
>>[1, 2, 3, 4, 5]

需要注意的是,它eval()可以執行任何有效的 Python 表達式,包括算術運算、函數調用,甚至是複雜的表達式。eval()但是,在使用使用者提供的輸入時應小心謹慎,因為如果輸入未經過正確驗證,可能會帶來安全風險。惡意輸入可能會執行任意程式碼。

hkweather report

假設你有兩天想製作一個人自動向天文台查詢天氣,那麼下你的例子面適合。

香港天文台本身設有一個api,可供各位自行查詢。下面是連結

https://data.weather.gov.hk/weatherAPI/doc/HKO_Open_Data_API_Documentation_tc.pdf
下面是一個數據的例子。

https://data.weather.gov.hk/weatherAPI/opendata/weather.php?dataType=flw&lang=tc

我先來解釋這條網址(url)的操作,dataType是一個主要的查詢方向,而lang是負責選擇語言。

DataType 查詢內容
flw 本港地區天氣預報
fnd 九天天氣預報
rhrread 本港地區天氣報告
warnsum 天氣警告一覽
warningInfo 詳細天氣警告資訊
swt 特別天氣提示
lang 語言
en 英文
tc 繁體中文
sc 簡體中文

在每個內容也有非常多的字典及字串,需要認真處理,才不會出現錯誤。

簡單嘗試:

import requests as req

url="https://data.weather.gov.hk/weatherAPI/opendata/weather.php"
response = req.get(url+"?dataType=flw&lang=tc")
n=eval(response.text)
print(type(n))
print(n["generalSituation"])
print(n["updateTime"])

執行結果:

一股微弱東北季候風正逐漸影響廣東沿岸,並為該區帶來驟雨及雷暴。此外,高空反氣旋正覆蓋南海北部。本港風勢微弱,正午時分大部分地區氣溫上升至32度左右。
2023-09-22T15:45:00+08:00

嘗試另外一個變數

import requests as req

url="https://data.weather.gov.hk/weatherAPI/opendata/weather.php"
#tc=traditional chinese
data="fnd"
lang="tc"
response = req.get(f"{url}?dataType={data}&lang={lang}")
n=eval(response.text)
print(type(n))
print(n["weatherForecast"])

執行結果:

[{'forecastDate': '20230923', 'week': '星期六', 'forecastWind': '東至東北風2至3級。', 'forecastWeather': '部分時間有陽光,局部地區有驟雨。日間酷熱。', 'forecastMaxtemp': {'value': 33, 'unit': 'C'}, 'forecastMintemp': {'value': 28, 'unit': 'C'}, 'forecastMaxrh': {'value': 90, 'unit': 'percent'}, 'forecastMinrh': {'value': 65, 'unit': 'percent'}, 'ForecastIcon': 53, 'PSR': '低'}, {'forecastDate': '20230924', 'week': '星期日', 'forecastWind': '東至東北風4級,離岸及高地間中5級。', 'forecastWeather': '短暫時間有陽光,有幾陣驟雨。', 'forecastMaxtemp': {'value': 32, 'unit': 'C'}, 'forecastMintemp': {'value': 28, 'unit': 'C'}, 'forecastMaxrh': {'value': 95, 'unit': 'percent'}, 'forecastMinrh': {'value': 70, 'unit': 'percent'}, 'ForecastIcon': 54, 'PSR': '低'}, {'forecastDate': '20230925', 'week': '星期一', 'forecastWind': '東至東北風4級,離岸間中5級。', 'forecastWeather': '部分時間有陽光。', 'forecastMaxtemp': {'value': 32, 'unit': 'C'}, 'forecastMintemp': {'value': 28, 'unit': 'C'}, 'forecastMaxrh': {'value': 90, 'unit': 'percent'}, 'forecastMinrh': {'value': 65, 'unit': 'percent'}, 'ForecastIcon': 51, 'PSR': '低'}, {'forecastDate': '20230926', 'week': '星期二', 'forecastWind': '東至東北風4級,初時離岸間中5級。', 'forecastWeather': '部分時間有陽光,局部地區有驟雨。', 'forecastMaxtemp': {'value': 32, 'unit': 'C'}, 'forecastMintemp': {'value': 28, 'unit': 'C'}, 'forecastMaxrh': {'value': 90, 'unit': 'percent'}, 'forecastMinrh': {'value': 65, 'unit': 'percent'}, 'ForecastIcon': 53, 'PSR': '低'}, {'forecastDate': '20230927', 'week': '星期三', 'forecastWind': '東至東北風3至4級。', 'forecastWeather': '短暫時間有陽光,有幾陣驟雨。', 'forecastMaxtemp': {'value': 32, 'unit': 'C'}, 'forecastMintemp': {'value': 28, 'unit': 'C'}, 'forecastMaxrh': {'value': 95, 'unit': 'percent'}, 'forecastMinrh': {'value': 75, 'unit': 'percent'}, 'ForecastIcon': 54, 'PSR': '低'}, {'forecastDate': '20230928', 'week': '星期四', 'forecastWind': '東至東北風4級,離岸間中5級。', 'forecastWeather': '大致多雲,有幾陣驟雨。日間短暫時間有陽光。', 'forecastMaxtemp': {'value': 31, 'unit': 'C'}, 'forecastMintemp': {'value': 27, 'unit': 'C'}, 'forecastMaxrh': {'value': 95, 'unit': 'percent'}, 'forecastMinrh': {'value': 70, 'unit': 'percent'}, 'ForecastIcon': 54, 'PSR': '中低'}, {'forecastDate': '20230929', 'week': '星期五', 'forecastWind': '北至東北風4至5級。', 'forecastWeather': '大致多雲,有幾陣驟雨。日間短暫時間有陽光。', 'forecastMaxtemp': {'value': 30, 'unit': 'C'}, 'forecastMintemp': {'value': 27, 'unit': 'C'}, 'forecastMaxrh': {'value': 95, 'unit': 'percent'}, 'forecastMinrh': {'value': 75, 'unit': 'percent'}, 'ForecastIcon': 54, 'PSR': '中低'}, {'forecastDate': '20230930', 'week': '星期六', 'forecastWind': '北至東北風4至5級。', 'forecastWeather': '大致多雲,有幾陣驟雨。日間短暫時間有陽光。', 'forecastMaxtemp': {'value': 30, 'unit': 'C'}, 'forecastMintemp': {'value': 26, 'unit': 'C'}, 'forecastMaxrh': {'value': 95, 'unit': 'percent'}, 'forecastMinrh': {'value': 75, 'unit': 'percent'}, 'ForecastIcon': 54, 'PSR': '中低'}, {'forecastDate': '20231001', 'week': '星期日', 'forecastWind': '北至東北風4至5級。', 'forecastWeather': '大致多雲,有幾陣驟雨。日間短暫時間有陽光。', 'forecastMaxtemp': {'value': 30, 'unit': 'C'}, 'forecastMintemp': {'value': 26, 'unit': 'C'}, 'forecastMaxrh': {'value': 95, 'unit': 'percent'}, 'forecastMinrh': {'value': 75, 'unit': 'percent'}, 'ForecastIcon': 54, 'PSR': '中低'}]

嘗試用另外一種語言取得內容

import requests as req

url="https://data.weather.gov.hk/weatherAPI/opendata/weather.php"
#tc=traditional chinese
data="warningInfo"
lang="en"
response = req.get(f"{url}?dataType={data}&lang={lang}")
n=eval(response.text)
print(type(n))
print(n["details"][0])

執行結果:

{'contents': ['The Very Hot Weather Warning has been issued by the Hong Kong Observatory at 11:15 a.m.', 'Very hot weather expected in Hong Kong today. The public should beware of heat stroke.', 'Elderly persons, pregnant women, infants and children, those with chronic illnesses, such as heart disease or high blood pressure, as well as the obese are more vulnerable to heat stroke. Watch out for any symptoms of heat stroke.', 'When engaged in work under hot weather or high-temperature environments, please refer to the Guidance Notes on Prevention of Heat Stroke at Work of the Labour Department and take necessary precautions.', 'When engaged in outdoor activities, drink more water and avoid over exertion. If not feeling well, take a rest in the shade or cooler place as soon as possible. Perform outdoor activities in the morning or late afternoon.', 'During indoor activities, replenish water timely. If there is no air-conditioning, then windows should be kept open and fans be used as far as possible to maintain adequate indoor ventilation. Public facilities with heat sheltering may also be used.', 'Avoid prolonged exposure under sunlight. Light-coloured and air-permeable clothing, wide-brimmed hats and ultraviolet (UV) blocking sunglasses can reduce the chance of sunburn by solar UV radiation.', 'Use a broad-spectrum sunscreen lotion of a Sun Protection Factor (SPF) 15 or above generously. Apply again if you stay out in the sun for more than two hours and after swimming, sweating, or towelling off.', 'Beware of health and wellbeing of elderly or persons with chronic medical conditions. If you know of them, call or visit them occasionally and check whether their household ventilation and air-conditioning devices function properly.', 'Those more vulnerable to heat stroke should avoid outdoor activities and stay away from hot environment as far as possible. Stay in cool, well-ventilated or air-conditioned places.'], 'warningStatementCode': 'WHOT', 'updateTime': '2023-09-22T11:15:00+08:00'}

今天就先到這裏,明天我會跟大家一起拆解這些複雜的內容,如果覺得我的文章對你有幫助或有更好的建議,可以追蹤我,可以按讚和不妨在留言區提出,明天再見吧。bye
/images/emoticon/emoticon82.gif
reference:
https://data.weather.gov.hk/weatherAPI/doc/HKO_Open_Data_API_Documentation_tc.pdf


上一篇
python基礎及數據科學之應用day 6[Python基礎應用(Requests模組提取網上內容及圖片)]
下一篇
python基礎及數據科學之應用day 8[Python基礎應用(Requests模組進階嘗試和webbrowser)]
系列文
python基礎及數據科學之應用30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言